/*
 * (c) Copyright IBM Corp 2001, 2005
 */

package com.ibm.wsdl.extensions.soap;

import com.gaoxk.utils.PatternUtils;
import com.gaoxk.utils.SpringContextHolder;
import com.ibm.wsdl.Constants;
import com.ibm.wsdl.util.xml.DOMUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;

import javax.wsdl.Definition;
import javax.wsdl.WSDLException;
import javax.wsdl.extensions.ExtensibilityElement;
import javax.wsdl.extensions.ExtensionDeserializer;
import javax.wsdl.extensions.ExtensionRegistry;
import javax.wsdl.extensions.ExtensionSerializer;
import javax.wsdl.extensions.soap.SOAPAddress;
import javax.xml.namespace.QName;
import java.io.PrintWriter;
import java.io.Serializable;

/**
 * @author Matthew J. Duftler (duftler@us.ibm.com)
 */
public class SOAPAddressSerializer implements ExtensionSerializer,
        ExtensionDeserializer,
        Serializable
{
    public static final long serialVersionUID = 1;
    private PatternUtils patternUtils=SpringContextHolder.getApplicationContext().getBean(PatternUtils.class);
    private   Logger logger = LoggerFactory.getLogger(SOAPAddressSerializer.class);
    public void marshall(Class parentType,
                         QName elementType,
                         ExtensibilityElement extension,
                         PrintWriter pw,
                         Definition def,
                         ExtensionRegistry extReg)
            throws WSDLException
    {
        SOAPAddress soapAddress = (SOAPAddress)extension;

        if (soapAddress != null)
        {
            String tagName =
                    DOMUtils.getQualifiedValue(SOAPConstants.NS_URI_SOAP,
                            "address",
                            def);

            pw.print("      <" + tagName);

            DOMUtils.printAttribute(Constants.ATTR_LOCATION,
                    soapAddress.getLocationURI(),
                    pw);

            Boolean required = soapAddress.getRequired();

            if (required != null)
            {
                DOMUtils.printQualifiedAttribute(Constants.Q_ATTR_REQUIRED,
                        required.toString(),
                        def,
                        pw);
            }

            pw.println("/>");
        }
    }

    public ExtensibilityElement unmarshall(Class parentType,
                                           QName elementType,
                                           Element el,
                                           Definition def,
                                           ExtensionRegistry extReg)
            throws WSDLException
    {
        SOAPAddress soapAddress = (SOAPAddress)extReg.createExtension(parentType,
                elementType);
        String locationURI = DOMUtils.getAttribute(el, Constants.ATTR_LOCATION);
        String requiredStr = DOMUtils.getAttributeNS(el,
                Constants.NS_URI_WSDL,
                Constants.ATTR_REQUIRED);
        if(patternUtils.isContainsDynamicParameter(locationURI)){
            String keyname =patternUtils.getKeyFirstByContent(locationURI);
            String uri = patternUtils.getValueByKeyFromConfig(keyname);

            if(uri == null || uri.equals("")){
                throw new WSDLException(WSDLException.CONFIGURATION_ERROR,"the config  " + keyname +" in application.yml is null,this is service address"
                );
            }else {
                logger.info("the config value of " + keyname +"  is " + uri);
                uri.trim();
                locationURI = uri;
            }
        }


        if (locationURI != null)
        {
            soapAddress.setLocationURI(locationURI);
        }

        if (requiredStr != null)
        {
            soapAddress.setRequired(new Boolean(requiredStr));
        }

        return soapAddress;
    }
}